package edu.xidian.bdilab.jrbigdata.service.enterprise.checkStatics.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseReq;
import edu.xidian.bdilab.jrbigdata.domain.FinancialProduct;
import edu.xidian.bdilab.jrbigdata.domain.ProductRequest;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseReqMapper;
import edu.xidian.bdilab.jrbigdata.mapper.FinancialProductMapper;
import edu.xidian.bdilab.jrbigdata.mapper.ProductRequestMapper;
import edu.xidian.bdilab.jrbigdata.service.enterprise.checkStatics.CheckStaticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CheckStaticsServiceImpl implements CheckStaticsService {

    @Autowired
    EnterpriseReqMapper enterpriseReqMapper;

    @Autowired
    ProductRequestMapper productRequestMapper;

    @Autowired
    FinancialProductMapper financialProductMapper;

    @Override
    public ResponseResult checkStatics(String socialCode){
        ResponseResult responseResult = new ResponseResult();
        Map<String,Object> data=new HashMap<>();

        //查出自己企业的所有公开需求
        QueryWrapper<EnterpriseReq> enterpriseReqQueryWrapper = new QueryWrapper<>();
        enterpriseReqQueryWrapper.eq("fk_social_code", socialCode)
                .eq("is_public", 0);
        List<EnterpriseReq> enterpriseReqList = enterpriseReqMapper.selectList(enterpriseReqQueryWrapper);
        //总发布需求数
        Integer enterpriseReqCount = enterpriseReqList.size();
        //总需求金额
        Integer enterpriseReqAmountCount=0;
        for(EnterpriseReq enterpriseReq: enterpriseReqList){
            switch (enterpriseReq.getLoanLimit()){
                case 0:enterpriseReqAmountCount+=100;break;
                case 1:enterpriseReqAmountCount+=200;break;
                case 2:enterpriseReqAmountCount+=300;break;
                case 3:enterpriseReqAmountCount+=500;break;
                default:enterpriseReqAmountCount+=1000;break;
            }
        }

        //查出自己企业的所有金融产品申请
        QueryWrapper<ProductRequest> productRequestQueryWrapper = new QueryWrapper<>();
        productRequestQueryWrapper.eq("fk_social_code", socialCode);
        List<ProductRequest> productRequestList = productRequestMapper.selectList(productRequestQueryWrapper);
        //总申请数。数量上等于成功+拒绝+申请中
        Integer productRequestCount = productRequestList.size();

        //成交数、成交金额、正在申请数、正在申请金额数
        Integer productRequestSuccessCount=0;
        Integer productRequestSuccessAmountCount=0;
        Integer productRequestApplyingCount=0;
        Integer productRequestApplyingAmountCount=0;
        for(ProductRequest productRequest: productRequestList){
            //通过申请名查询指定产品额度
            FinancialProduct financialProduct = financialProductMapper
                    .selectById(productRequest.getFkProductName());

            if(productRequest.getStatus()==0){
                //0表示申请中
                productRequestApplyingCount++;
                switch (financialProduct.getLoanLimit()){
                    case 0:productRequestApplyingAmountCount+=100;break;
                    case 1:productRequestApplyingAmountCount+=200;break;
                    case 2:productRequestApplyingAmountCount+=300;break;
                    case 3:productRequestApplyingAmountCount+=500;break;
                    default:productRequestApplyingAmountCount+=1000;break;
                }
            }
            else if(productRequest.getStatus()==1){
                //1表示成交
                productRequestSuccessCount++;
                switch (financialProduct.getLoanLimit()){
                    case 0:productRequestSuccessAmountCount+=100;break;
                    case 1:productRequestSuccessAmountCount+=200;break;
                    case 2:productRequestSuccessAmountCount+=300;break;
                    case 3:productRequestSuccessAmountCount+=500;break;
                    default:productRequestSuccessAmountCount+=1000;break;
                }
            }
        }

        data.put("enterpriseReqCount",enterpriseReqCount);
        data.put("enterpriseReqAmountCount",enterpriseReqAmountCount);
        data.put("productRequestCount",productRequestCount);
        data.put("productRequestSuccessCount",productRequestSuccessCount);
        data.put("productRequestSuccessAmountCount",productRequestSuccessAmountCount);
        data.put("productRequestApplyingCount",productRequestApplyingCount);
        data.put("productRequestApplyingAmountCount",productRequestApplyingAmountCount);


        responseResult.setData(data);
        responseResult.setMessage("企业数据查询成功");
        responseResult.setSuccess(true);
        return responseResult;


    }

}
